const {Tool, API, Event, Storage} = global.$util
Component({
    properties: {
        door: {
            type: Number,
            value: 1,
        },
        maxLength: {
            type: Number,
            value: 4,
        },
        visible: Boolean,
        region:{
            type:Object,
            value: {}
        }
    },
    data: {
        list: [],
        activeIndex: 0,
        selectAreaList: [],
        loading:false
    },
    methods: {
        /**
         *  获取省市区
         */
        queryAreaList (fatherCode, changeArea){
            let {
                selectAreaList,
                activeIndex,
                door,
                maxLength,
                loading
            } = this.data

            if(loading) return
            this.data.loading = true
            const selectAreaListLength = selectAreaList.length

            if (door == 2 && selectAreaListLength == maxLength && !changeArea) {
                this.saveAreaListResult()
                return
            }

            API.queryAreaList({
                fatherCode
            }).then((res) => {
                this.data.loading = false
                let list = res.data || []
                if (list.length) {
                    if (selectAreaListLength < maxLength) {
                        selectAreaList.push({
                            name: '请选择'
                        })
                        activeIndex = selectAreaList.length - 1
                    }

                    this.setData({
                        list,
                        selectAreaList,
                        activeIndex
                    })
                } else {
                    this.saveAreaListResult()
                }
            }).catch((res) => {
                this.data.loading = false
                console.log(res)
            })
        },
        /**
         *  点击选择省市区
         */
        areaListClick (e){
            let index = e.currentTarget.dataset.index
            let item = this.data.list[index]
            let {
                activeIndex,
                selectAreaList
            } = this.data
            selectAreaList[activeIndex] = item

            if ((selectAreaList[activeIndex + 1] || {}).code) {
                selectAreaList.splice(activeIndex + 1)
            }

            this.setData({
                selectAreaList,
            }, ()=> {
                this.queryAreaList(item.code)
            })

        },
        /**
         *  点击已选择的省市区
         */
        selectListClick  (e){
            let index = e.currentTarget.dataset.index
            let item = this.data.selectAreaList[index]
            if (!item.code) return
            wx.pageScrollTo({
                scrollTop: 0
            })
            this.setData({
                activeIndex: index
            }, ()=> {
                this.queryAreaList(item.fatherCode, true)
            })
        },
        /**
         *  保存所选省市区的结果,并回到上级页面
         */
        saveAreaListResult() {
            const {
                selectAreaList
            } = this.data
            let areaList = {
                province: selectAreaList[0].name,
                provinceCode: selectAreaList[0].code,
                city: selectAreaList[1].name,
                cityCode: selectAreaList[1].code,
                area: selectAreaList[2].name,
                areaCode: selectAreaList[2].code,
                street: selectAreaList[3] ? selectAreaList[3].name : '',
                streetCode: selectAreaList[3] ? selectAreaList[3].code : '',
            }
            this.closeClick()
            this.triggerEvent('saveAreaListResult', areaList);
        },
        closeClick(){
            this.triggerEvent('close', false)
        }
    },
    ready(){
        const {
            province,
            provinceCode,
            city,
            cityCode,
            area,
            areaCode,
            street,
            streetCode
        } = this.data.region

        if(this.data.region.province){
            let list = [
                {name:province,code:provinceCode,fatherCode:0},
                {name:city,code:cityCode,fatherCode:provinceCode},
                {name:area,code:areaCode,fatherCode:cityCode},
                {name:street,code:streetCode,fatherCode:areaCode}
            ]
            let selectAreaList = list.filter((item)=>{
                return item.code
            })
            this.setData({
                selectAreaList,
                activeIndex:selectAreaList.length-1
            })
            this.queryAreaList(selectAreaList[selectAreaList.length-1].fatherCode,true)
        }else{
           this.queryAreaList(0)
        }
    }
})
